package TuiLayer;
import ControlLayer.CustomerCtr;
import ModelLayer.Customer;
import java.util.Scanner;
/**
 * Vestbjerg project
 * @GroupB2 (UCN - Computer Science DMAJ0914) 
 */
public class CustomerUI
{
    // instance variables
    private CustomerCtr customerCtr;
    private Customer foundCustomer;

    /**
     * Constructor for objects of class CustomerUI
     */
    public CustomerUI()
    {
        customerCtr = new CustomerCtr();
    }

    public void createCustomer(String id, String name, String address, String phone, String email, String type) 
    {
        customerCtr.createCustomer(id,name,address,phone,email,type);
    }

    public Customer findCustomer(String id) 
    {
        return customerCtr.findCustomer(id);
    }

    public void updateCustomer(Customer customer,String id, String name, String address, String phone, String email, String type) 
    {
        customerCtr.updateCustomer(customer,id, name, address, phone, email, type);
    }

    public void deleteCustomer(Customer customer) 
    {
        customerCtr.deleteCustomer(customer);
    }

    private int getChoice()
    {
        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        return choice;
    }

    // Customers menu
    public void menuStart() 
    {
        boolean exit = false;
        String print = null;
        while(!exit) 
        {
            writeCustomersMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = customerAddStart();
            }
            else if(choice == 2)
            {
                print = browseMenuStart();
            }
            else if(choice == 0) 
            {
                exit = true;
            }

        }
    }

    public String browseMenuStart() 
    {

        boolean exit = false;
        String print = null;
        print =  customerFindStart();
        if(foundCustomer == null) 
        {
            exit = true;
        }
        while(!exit) 
        {
            writeBrowseCustomersMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = customerUpdateStart();
            }
            else if(choice == 2)
            {
                print = customerRemoveStart();

            }
            else if(choice == 0) 
            {
                print = null;
                exit = true;
            }

        }
        return print;
    }

    // Write friends menu
    public void writeCustomersMenu(String print) 
    {
        System.out.println("\f *** Customers ***");
        System.out.println(" (1)Add");
        System.out.println(" (2)Find / Edit");
        System.out.println(" (0)Back");

        if(print != null) 
        {
            System.out.println("\n " + print);

        }

        System.out.print("\n Make your choice: ");
    }

    public void writeBrowseCustomersMenu(String print) 
    {
        System.out.println("\f *** Customer ID: " + foundCustomer.getId() + " ***");
        System.out.println(" (1)Edit");
        System.out.println(" (2)Delete");
        System.out.println(" (0)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    // Start "Add a customer"
    public String customerAddStart() 
    {
        String print = null;
        System.out.println("\nEnter ID: ");
        Scanner sc = new Scanner(System.in);
        String id = sc.nextLine();

        System.out.println("Enter name: ");
        String name = sc.nextLine();

        System.out.println("Enter address: ");
        String address = sc.nextLine();

        System.out.println("Enter phone: ");
        String phone = sc.nextLine();

        System.out.println("Enter email: ");
        String email = sc.nextLine();

        System.out.println("Enter type: ");
        String type = sc.nextLine();

        Customer customer = customerCtr.findCustomer(id);

        if(customer == null)
        {
            createCustomer(id,name,address,phone,email,type);
            print = "Customer #" + id + " has been added to your Customers.";
        }
        else 
        {
            print = "Customer already exists.";
        }

        return print;
    }

    // Start "Find a customer"
    public String customerFindStart() 
    {
        String print = null;
        System.out.print("\nEnter ID: ");

        Scanner sc = new Scanner(System.in);
        String id = sc.next();

        foundCustomer = findCustomer(id);
        if(foundCustomer != null) 
        {
            print = " # Found Customer: #" + "\n" 
            + "  ID: " + foundCustomer.getId() + "\n"
            + "  Name: " + foundCustomer.getName() + "\n"
            + "  Address: " + foundCustomer.getAddress() + "\n"
            + "  Phone: " + foundCustomer.getPhone() + "\n"
            + "  E-mail: " + foundCustomer.getEmail() + "\n";
        }
        else 
        {
            print = "ERROR: Customer not found.";
        }
        return print;
    }
    // Start "Update a customer"
    public String customerUpdateStart() 
    {
        String print = null;
        System.out.println("\nEnter new ID: ");
        Scanner sc = new Scanner(System.in);
        String newId = sc.nextLine();

        System.out.println("Enter new name: ");
        String newName = sc.nextLine();

        System.out.println("Enter new address: ");
        String newAddress = sc.nextLine();

        System.out.println("Enter new phone: ");
        String newPhone = sc.nextLine();

        System.out.println("Enter new email: ");
        String newEmail = sc.nextLine();

        System.out.println("Enter new type: ");
        String newType = sc.nextLine();

        updateCustomer(foundCustomer,newId,newName,newAddress,newPhone,newEmail,newType);

        print = "Customer #" + newId + " has been updated to:\n" + getCustomerInfo(foundCustomer);

        return print;
    }

    private String getCustomerInfo(Customer customer) 
    {
        String string = "  ID: " + customer.getId() + "\n"
            + "  Name: " + customer.getName() + "\n"
            + "  Address: " + customer.getAddress() + "\n"
            + "  Phone: " + customer.getPhone() + "\n"
            + "  Email: " + customer.getEmail() + "\n"
            + "  Type: " + customer.getType() + "\n";
        return string;
    }

    // Start "Remove a customer"
    public String customerRemoveStart() 
    {
        String print = null;
        deleteCustomer(foundCustomer);
        print = "Customer #" + foundCustomer.getId() + " has been deleted.";

        return print;
    }
}
